package cn.shop.dys.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtils {
	/*
	 * 多线程下载
	 */
	/*
	 * public static HttpURLConnection getRangeGetHttpURLConnection(URL url, int
	 * begin, int end) throws IOException { HttpURLConnection openConnection =
	 * (HttpURLConnection) url .openConnection();
	 * openConnection.setRequestMethod("GET"); openConnection
	 * .setRequestProperty("Range", "bytes:" + begin + "-" + end);
	 * openConnection.setConnectTimeout(5000); return openConnection; }
	 */

	/*
	 * Get请求
	 */
	public static HttpURLConnection getGetHttpURLConnection(URL url) throws IOException {
		HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
		openConnection.setRequestMethod("GET");
		openConnection.setConnectTimeout(3000);
		openConnection.setReadTimeout(3000);
		return openConnection;
	}

	/*
	 * Post请求
	 */
	/*
	 * public static HttpURLConnection getPostHttpURLConnection(URL url, byte[]
	 * data) throws IOException { HttpURLConnection openConnection =
	 * (HttpURLConnection) url .openConnection();
	 * openConnection.setRequestMethod("POST");
	 * openConnection.setRequestProperty("Content-Type",
	 * "application/x-www-form-urlencoded");
	 * openConnection.setRequestProperty("Content-Length", "" + data.length);
	 * openConnection.setConnectTimeout(5000); openConnection.setDoOutput(true);
	 * openConnection.getOutputStream().write(data); return openConnection; }
	 */

	/*
	 * Head请求
	 * 
	 * public static HttpURLConnection getHeadHttpURLConnection(URL url) throws
	 * IOException { HttpURLConnection openConnection = (HttpURLConnection) url
	 * .openConnection(); openConnection.setRequestMethod("HEAD");
	 * openConnection.setConnectTimeout(5000); return openConnection; }
	 */
	public static boolean isConnectedInternet(Context context) {
		boolean isConnectedInternet = false;
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		// State state = networkInfo.getState();
		if (networkInfo == null) {
			return false;
		}
		int type = networkInfo.getType();
		switch (type) {
		case ConnectivityManager.TYPE_MOBILE:
		case ConnectivityManager.TYPE_WIFI:
			isConnectedInternet = true;
			break;
		default:
			break;
		}

		return isConnectedInternet;
	}

}
